package com.linuxgt.android.service;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.util.Log;

import com.linuxgt.android.domain.Person;
import com.linuxgt.android.xmlapp.R;

import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import static org.junit.Assert.*;

/**
 * Created by liute on 2017/5/15.
 */
public class PersonServiceTest {

    private Context context;
    private PersonService personService;

    @Test
    public void getPersons() throws Exception {
        InputStream inputStream = context.getResources().openRawResource(R.raw.person);
        List<Person> personList = personService.getPersons(inputStream);
        assertNotNull(personList);
        for (Person person : personList) {
            Log.i(getClass().getName(), person.toString());
        }
    }

    @Test
    public void savePersons() throws Exception {
        List<Person> personList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            personList.add(new Person("00" + i, "test" + i, i));
        }
        personService.save(personList,new FileOutputStream(new File(context.getFilesDir(),"person.xml")));
    }

    @Test
    public void saveObject() throws Exception {
        List<Person> personList = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            personList.add(new Person("00" + i, "test" + i, i));
        }
        personService.saveObject(personList,new FileOutputStream(new File(context.getFilesDir(),"personObject.xml")));
    }

    @Test
    public void charset() throws Exception {
        Set<String> charsetNames = Charset.availableCharsets().keySet();
        Iterator<String> iterator = charsetNames.iterator();
        while (iterator.hasNext()) {
            Log.i(getClass().getName(), iterator.next());
        }
    }

    @Before
    public void init() throws Exception {
        personService = new PersonService();
        context = InstrumentationRegistry.getTargetContext();
    }
}